﻿using System;
using System.Collections;
using System.Configuration;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
using System.Collections.Generic;
/// <summary>
///MYSQLHelper 的摘要说明
/// </summary>
public abstract class MySqlHelper
{
    //数据库连接字符串
    public static string Conn = "Database='mydb';Data Source='localhost';User Id='root';Password='leopard';charset='utf8';pooling=true;port=3306";

    // 用于缓存参数的HASH表
    private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

    /// <summary>
    ///  给定连接的数据库用假设参数执行一个sql命令（不返回数据集）
    /// </summary>
    /// <param name="connectionString">一个有效的连接字符串</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>执行命令所影响的行数</returns>
    public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {

        MySqlCommand cmd = new MySqlCommand();

        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
            int val = cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
            return val;
        }
    }
    /// <summary>
    /// 给定连接的数据库用假设参数执行一个sql命令（不返回数据集）
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <returns></returns>
    public static int ExecuteNonQuery(string sql)
    {
        return  ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text,sql,null);
    }
    

    /// <summary>
    /// 用现有的数据库连接执行一个sql命令（不返回数据集）
    /// </summary>
    /// <param name="connection">一个现有的数据库连接</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>执行命令所影响的行数</returns>
    public static int ExecuteNonQuery(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {

        MySqlCommand cmd = new MySqlCommand();

        PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
        int val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        return val;
    }

    /// <summary>
    ///使用现有的SQL事务执行一个sql命令（不返回数据集）
    /// </summary>
    /// <remarks>
    ///举例:
    ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
    /// </remarks>
    /// <param name="trans">一个现有的事务</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>执行命令所影响的行数</returns>
    public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {
        MySqlCommand cmd = new MySqlCommand();
        PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
        int val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        return val;
    }

    /// <summary>
    /// 用执行的数据库连接执行一个返回数据集的sql命令
    /// </summary>
    /// <remarks>
    /// 举例:
    ///  MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
    /// </remarks>
    /// <param name="connectionString">一个有效的连接字符串</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>包含结果的读取器</returns>
    public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {
        //创建一个MySqlCommand对象
        MySqlCommand cmd = new MySqlCommand();
        //创建一个MySqlConnection对象
        MySqlConnection conn = new MySqlConnection(connectionString);

        //在这里我们用一个try/catch结构执行sql文本命令/存储过程，因为如果这个方法产生一个异常我们要关闭连接，因为没有读取器存在，
        //因此commandBehaviour.CloseConnection 就不会执行
        try
        {
            //调用 PrepareCommand 方法，对 MySqlCommand 对象设置参数
            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
            //调用 MySqlCommand  的 ExecuteReader 方法
            MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            //清除参数
            cmd.Parameters.Clear();
            return reader;
        }
        catch
        {
            //关闭连接，抛出异常
            conn.Close();
            throw;
        }
    }

    /// <summary>
    /// 返回DataSet
    /// </summary>
    /// <param name="connectionString">一个有效的连接字符串</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns></returns>
    public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {
        //创建一个MySqlCommand对象
        MySqlCommand cmd = new MySqlCommand();
        //创建一个MySqlConnection对象
        MySqlConnection conn = new MySqlConnection(connectionString);

        //在这里我们用一个try/catch结构执行sql文本命令/存储过程，因为如果这个方法产生一个异常我们要关闭连接，因为没有读取器存在，

        try
        {
            //调用 PrepareCommand 方法，对 MySqlCommand 对象设置参数
            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
            //调用 MySqlCommand  的 ExecuteReader 方法
            MySqlDataAdapter adapter = new MySqlDataAdapter();
            adapter.SelectCommand = cmd;
            DataSet ds = new DataSet();

            adapter.Fill(ds);
            //清除参数
            cmd.Parameters.Clear();
            conn.Close();
            return ds;
        }
        catch (Exception e)
        {
            string s = e.Message;
            throw e;
        }
    }



    /// <summary>
    /// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列
    /// </summary>
    /// <remarks>
    ///例如:
    ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
    /// </remarks>
    ///<param name="connectionString">一个有效的连接字符串</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns>
    public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {
        MySqlCommand cmd = new MySqlCommand();

        using (MySqlConnection connection = new MySqlConnection(connectionString))
        {
            PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
            object val = cmd.ExecuteScalar();
            cmd.Parameters.Clear();
            return val;
        }
    }

    /// <summary>
    /// 用指定的数据库连接执行一个命令并返回一个数据集的第一列
    /// </summary>
    /// <remarks>
    /// 例如:
    ///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
    /// </remarks>
    /// <param name="connection">一个存在的数据库连接</param>
    /// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
    /// <param name="cmdText">存储过程名称或者sql命令语句</param>
    /// <param name="commandParameters">执行命令所用参数的集合</param>
    /// <returns>用 Convert.To{Type}把类型转换为想要的 </returns>
    public static object ExecuteScalar(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {

        MySqlCommand cmd = new MySqlCommand();

        PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
        object val = cmd.ExecuteScalar();
        cmd.Parameters.Clear();
        return val;
    }

    /// <summary>
    /// 将参数集合添加到缓存
    /// </summary>
    /// <param name="cacheKey">添加到缓存的变量</param>
    /// <param name="commandParameters">一个将要添加到缓存的sql参数集合</param>
    public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters)
    {
        parmCache[cacheKey] = commandParameters;
    }

    /// <summary>
    /// 找回缓存参数集合
    /// </summary>
    /// <param name="cacheKey">用于找回参数的关键字</param>
    /// <returns>缓存的参数集合</returns>
    public static MySqlParameter[] GetCachedParameters(string cacheKey)
    {
        MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey];

        if (cachedParms == null)
            return null;

        MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length];

        for (int i = 0, j = cachedParms.Length; i < j; i++)
            clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone();

        return clonedParms;
    }

    public struct ManagerInfo
    {
        public string user;
        public string password;
        public string name;
        public string id;
    }
    /// <summary>
    /// 客户列表
    /// </summary>
    public struct UserInfo
    {
        public string id;
        public string user;
        public string password;
        public string value;
        public string name;
        public string phoneNumber;
        public string phoneNumberShort;
    }
    /// <summary>
    /// 获取单个用户的信息结构体
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static ManagerInfo GetSingleInfo(DataTable dt)
    {
        ManagerInfo m = new ManagerInfo();
        m.id = dt.Rows[0][0].ToString();
        m.user = dt.Rows[0][1].ToString();
        m.password = dt.Rows[0][2].ToString();
        m.name = dt.Rows[0][3].ToString();
        return m;
    }
    /// <summary>
    /// 获取多个用户的信息结构体
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static ManagerInfo[] GetInfo(DataTable dt)
    {
        ManagerInfo[] m = new ManagerInfo[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            m[i].id = dt.Rows[i][0].ToString();
            m[i].user = dt.Rows[i][1].ToString();
            m[i].password = dt.Rows[i][2].ToString();
            m[i].name = dt.Rows[i][3].ToString();
            //m.user;
        }
        return m;
    }
    /// <summary>
    /// 获取客户信息结构体
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static UserInfo[] GetUserInfo(DataTable dt)
    {
        UserInfo[] m = new UserInfo[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            m[i].id = dt.Rows[i][0].ToString();
            m[i].user = dt.Rows[i][1].ToString();
            m[i].password = dt.Rows[i][2].ToString();
            m[i].name = dt.Rows[i][3].ToString();
            m[i].value = dt.Rows[i][4].ToString();
            m[i].phoneNumber = dt.Rows[i][5].ToString();
            m[i].phoneNumberShort = dt.Rows[i][6].ToString();
            //m.user;
        }
        return m;
    }

    /// <summary>
    /// 准备执行一个命令
    /// </summary>
    /// <param name="cmd">sql命令</param>
    /// <param name="conn">OleDb连接</param>
    /// <param name="trans">OleDb事务</param>
    /// <param name="cmdType">命令类型例如 存储过程或者文本</param>
    /// <param name="cmdText">命令文本,例如:Select * from Products</param>
    /// <param name="cmdParms">执行命令的参数</param>
    private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
    {

        if (conn.State != ConnectionState.Open)
            conn.Open();

        cmd.Connection = conn;
        cmd.CommandText = cmdText;

        if (trans != null)
            cmd.Transaction = trans;

        cmd.CommandType = cmdType;

        if (cmdParms != null)
        {
            foreach (MySqlParameter parm in cmdParms)
                cmd.Parameters.Add(parm);
        }
    }

    public static void update(string table, string key, string oldValue, string newValue)
    {
        //string sql = "update "+table+ " set '"+value+"' where " + key + " =";
        //ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, "insert into myTestTable value (1,'李晓文')", null);
    }

    public enum Result
    {
        /// <summary>
        /// 引发了sql异常
        /// </summary>
        sqlError = -2,
        /// <summary>
        /// 非法的参数
        /// </summary>
        paramError = -1,
        /// <summary>
        /// 执行完成
        /// </summary>
        ok = 0
    };
    /// <summary>
    /// 对一个表插入一些值，返回结果是Result枚举常量.
    /// 你可以通过 Enum MySqlHelper.Result 判断他们
    /// </summary>
    /// <param name="table">要插入数据的表</param>
    /// <param name="keys">键</param>
    /// <param name="values">值</param>
    /// <returns></returns>
    public static Result insert(string table, string[] keys, string[] values)
    {
        // 检查参数合法性

        if (keys.Length != values.Length && keys != null && values != null)
            return Result.paramError;

        string sql = "insert into " + table + " ";
        string k = ""; string v = "";

        k = keys[0];
        v = values[0];
        // 从1开始遍历,向后添加
        for (int i = 1; i < keys.Length; i++)
        {
            k = k + "," + keys[i];
            v = v + "," + values[i];
        }
        sql = sql + "(" + k + ") " + "values (" + v + ") ";
        try
        {
            MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, null);
            return Result.ok;
        }
        catch (Exception e)
        {
            Console.Write(e.Message);
            return Result.sqlError;
        }
    }
    public static Result insert(string table, string[] values)
    {
        // 检查参数合法性

        if (values == null)
            return Result.paramError;

        string sql = "insert into " + table + " ";
        string k = ""; string v = "";

        v = values[0];
        // 从1开始遍历,向后添加
        for (int i = 1; i < values.Length; i++)
        {
            v = v + "," + values[i];
        }
        sql = sql + "values (" + v + ") ";
        Console.Write(sql);
        try
        {
           
            MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, null);
            return Result.ok;
        }
        catch (Exception e)
        {
            Console.Write(e.Message);
            return Result.sqlError;
        }
    }
    public static Result insert(string table, List<string> values) 
    {
        string[] s = values.ToArray();
        return insert(table, values.ToArray());
    }
    public static Result insert(string table, List<string> keys, List<string> values)
    {
        return insert(table, keys.ToArray(), values.ToArray());
    }
    /// <summary>
    /// 用 表，账户，密码 匹配远端用户数据库，匹配成功true 失败false。
    /// </summary>
    /// <param name="table"></param>
    /// <param name="id"></param>
    /// <param name="pw"></param>
    /// <param name="info">当返回值为真时，该结构体有值。</param>
    /// <returns></returns>
    public static bool mateUserInfo(string table, string id, string pw,out ManagerInfo info)
    {
        DataSet ds;
        //////////////////////// 构造sql语句
        string sql = string.Format("select * from where 账号='{0}' 密码='{1}' ",id,pw);

        //////////////////////// 读数据库
        try
        {
            ds = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, sql + table, null);//.Tables[0].DefaultView;
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.Write(ex.Message);
#endif
            info = new ManagerInfo();
            return false;
        }
        if (ds.Tables[0].Rows.Count > 0)
        {
            // 如果匹配到了数据
            info = MySqlHelper.GetSingleInfo(ds.Tables[0]);
            return true;
        }
        info = new ManagerInfo();
        return false;
    }

    public static bool userInfo(string table,string id, out UserInfo info)
    {
        DataSet ds;
        // 读数据库
        try
        {
            ds = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, "select * from " + table, null);//.Tables[0].DefaultView;
        }
        catch (Exception ex)
        {
#if DEBUG
            Console.Write(ex.Message);
#endif
            info = new UserInfo();
            return false;
        }
        MySqlHelper.UserInfo[] m = MySqlHelper.GetUserInfo(ds.Tables[0]);
        for (int i = 0; i < m.Length; i++)
        {
            // 匹配账号
            if (id.Equals(m[i].user))
            {
                ////// 有这个账号 ///////////////
                info = m[i];
                return true;
            }
        }
        info = new UserInfo();
        return false;
    }
}